package com.dtwenty.gaming.client.widgets;

import com.dtwenty.gaming.data.PlayerCharacter;
import com.dtwenty.gaming.data.PlayerCharacter.Size;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class InformationPanel extends CharacterSheetBox {

//	private DisclosurePanel panel = new DisclosurePanel();
	
	private PlayerCharacter character;
	
	private TextBox playerName;
	private TextBox characterName;
	private TextBox level;
	private TextBox characterClass;
	private TextBox paragonPath;
	private TextBox epicDestiny;
	private TextBox totalXP;
	private TextBox race;
	private ListBox size;
	private TextBox age;
	private ListBox gender;
	private TextBox height;
	private TextBox weight;
	private ListBox alignment;
	private TextBox deity;
	private TextBox adventuringCompany;
	
	public InformationPanel(PlayerCharacter character) {
		this.character = character;
		setup();
	}
	
	private void setup() {
		VerticalPanel subPanel = new VerticalPanel();
		HorizontalPanel row1 = new HorizontalPanel();
		HorizontalPanel row2 = new HorizontalPanel();
		HorizontalPanel row3 = new HorizontalPanel();

		playerName = new TextBox();
		playerName.setVisibleLength(20);
		playerName.setValue(character.getPlayerName());
		playerName.setEnabled(false);
		
		characterName = new TextBox();
		characterName.setVisibleLength(20);
		characterName.setValue(character.getCharacterName());
		characterName.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent ev) {
				character.setCharacterName(characterName.getValue());
			}
		});
		
		level = new TextBox();
		level.setMaxLength(2);
		level.setVisibleLength(1);
		level.setValue(character.getLevel().toString());
		level.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent ev) {
				try {
					character.setLevel(new Integer(level.getValue()));
				} catch (Exception e) {
					level.setValue(character.getLevel().toString());
				}
			}
		});
		
		characterClass = new TextBox();
		characterClass.setVisibleLength(15);
		characterClass.setValue(character.getCharacterClass());
		characterClass.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent ev) {
				character.setCharacterClass(characterClass.getValue());
			}
		});
		
		paragonPath = new TextBox();
		paragonPath.setVisibleLength(15);
		paragonPath.setValue(character.getParagonPath());
		paragonPath.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent ev) {
				character.setParagonPath(paragonPath.getValue());
			}
		});
		
		epicDestiny = new TextBox();
		epicDestiny.setVisibleLength(15);
		epicDestiny.setValue(character.getEpicDestiny());
		epicDestiny.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent ev) {
				character.setEpicDestiny(epicDestiny.getValue());
			}
		});
		
		totalXP = new TextBox();
		totalXP.setValue(character.getTotalXP().toString());
		totalXP.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent ev) {
				try {
					character.setTotalXP(new Integer(totalXP.getValue()));
				} catch (Exception e) {
					totalXP.setValue(character.getTotalXP().toString());
				}
			}
		});
		
		race = new TextBox();
		race.setVisibleLength(15);
		race.setValue(character.getRace());
		race.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent ev) {
				character.setRace(race.getValue());
			}
		});
		
		size = new ListBox(false);
		size.addItem("Small", PlayerCharacter.Size.SMALL.toString());
		size.addItem("Medium", PlayerCharacter.Size.MEDIUM.toString());
		size.addItem("Large", PlayerCharacter.Size.LARGE.toString());
		for(int i = 0; i < size.getItemCount(); i++)
			if(size.getValue(i).equals(character.getSize().toString()))
				size.setSelectedIndex(i);
		size.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent ev) {
				switch (size.getSelectedIndex()) {
				case 0:
					character.setSize(Size.SMALL);
					break;
				case 1:
					character.setSize(Size.MEDIUM);
					break;
				case 2:
					character.setSize(Size.LARGE);
					break;
				}
			}
		});
		
		deity = new TextBox();
		deity.setVisibleLength(15);
		deity.setValue(character.getDeity());
		deity.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent ev) {
				character.setDeity(epicDestiny.getValue());
			}
		});
		
		adventuringCompany = new TextBox();
		adventuringCompany.setVisibleLength(15);
		adventuringCompany.setValue(character.getAdventuringCompany());
		adventuringCompany.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent ev) {
				character.setAdventuringCompany(adventuringCompany.getValue());
			}
		});
		row1.add(new D20Label("Player Name : ", playerName, D20Label.Position.LEFT));
		
		row2.add(new D20Label("Character Name", characterName, D20Label.Position.BOTTOM));
		row2.add(new D20Label("Level", level, D20Label.Position.BOTTOM));
		row2.add(new D20Label("Class", characterClass, D20Label.Position.BOTTOM));
		row2.add(new D20Label("Paragon Path", paragonPath, D20Label.Position.BOTTOM));
		row2.add(new D20Label("Epic Destiny", epicDestiny, D20Label.Position.BOTTOM));
		row2.add(new D20Label("Total XP", totalXP, D20Label.Position.BOTTOM));
		
		row3.add(new D20Label("Race", race, D20Label.Position.BOTTOM));
		row3.add(new D20Label("Size", size, D20Label.Position.BOTTOM));
		row3.add(new D20Label("Deity", deity, D20Label.Position.BOTTOM));
		row3.add(new D20Label("Adventuring Company or Other Affiliations", adventuringCompany, D20Label.Position.BOTTOM));
		
		subPanel.add(row1);
		subPanel.add(row2);
		subPanel.add(row3);
		
		setBodyContent(subPanel);
		setHeaderText("DTWENTY CHARACTER SHEET");
	}
}
